package com.ruoyi.womp.mqhandler;

import java.text.ParseException;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.womp.config.RabbitMQConfig;
import com.ruoyi.womp.socket.WompWebSocket;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class RealTimeDataMQHandler3 {

	@Autowired
	MongoTemplate mongoTemplate;

	@Autowired
	WompWebSocket wompWebSocket;

	@RabbitListener(queues = { RabbitMQConfig.MQ_QUEUE_REALTIME_DATA })
	public void process(String data) {
		try {
			main(data);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void main(String data) throws ParseException {
		log.info("消费都3");
		log.info(data);
		// 转为Json对象
		JSONObject dataJsonObject = JSONObject.parseObject(data);
		// 消息类型
		Integer dataType = dataJsonObject.getInteger("type");
		// 消息实体
		JSONObject messageJsonObject = dataJsonObject.getJSONObject("message");
		// 衡器号
		String wacode = messageJsonObject.getString("waCode").trim();
		// 向web端发送数据
		wompWebSocket.pushMessage(wacode, data);
		// 实时数据
		if (dataType == 1) {
			JSONObject tableNameJsonObject = WompDataUtil.getDataTableName(messageJsonObject);

			JSONArray dataJsonArray = messageJsonObject.getJSONArray("data");
			if (dataJsonArray.size() > 0) {
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("waCode", wacode);
				jsonObject.put("time", messageJsonObject.getString("time"));

				for (int i = 0; i < dataJsonArray.size(); i++) {
					JSONObject dataJson = dataJsonArray.getJSONObject(i);

					jsonObject.put(dataJson.getString("code") + dataJson.getString("orderNum"),
							dataJson.getDouble("value"));
				}
				// 存入秒数据表
				mongoTemplate.insert(jsonObject, tableNameJsonObject.getString("secondName"));
				// 当前数据为分钟整点数据，存入分钟数据表
				if (messageJsonObject.getString("time").endsWith(":00")) {
					mongoTemplate.insert(jsonObject, tableNameJsonObject.getString("minuteName"));
				}
			}
		} else if (dataType == 2) {

			JSONArray dataJsonArray = messageJsonObject.getJSONArray("data");
			for (Object object : dataJsonArray) {
				JSONObject jsonObject = JSONObject.parseObject(object.toString());
				JSONObject alarmJsonObject = new JSONObject();
				alarmJsonObject.put("waCode", wacode);
				alarmJsonObject.put("time", messageJsonObject.getString("time"));
				alarmJsonObject.put("code", jsonObject.getString("code"));
				alarmJsonObject.put("value", jsonObject.getDouble("value"));
				if (jsonObject.containsKey("orderNum")) {
					alarmJsonObject.put("orderNum", jsonObject.getInteger("orderNum"));
				}
				alarmJsonObject.put("message", jsonObject.getString("message"));
				alarmJsonObject.put("state", 0);

				mongoTemplate.insert(alarmJsonObject, "womp_alarm_data");
			}
		}
	}
}
